package org.whatever.aha.zjut.platform.aspect;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.beetl.android.internal.util.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.annotation.CheckAcademyAdminModifyProject;
import org.whatever.aha.zjut.platform.annotation.ProjectPermission;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.entity.project.Project;
import org.whatever.aha.zjut.platform.service.AcademyAdminService;
import org.whatever.aha.zjut.platform.service.StudentInfoService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageStatusService;
import org.whatever.aha.zjut.platform.service.project.ProjectMemberService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.util.CheckUtil;
import org.whatever.aha.zjut.platform.vo.project.ProjectDetailVo;

import java.util.Date;
import java.util.List;

/**
 * @author Vc
 * @version 1.0
 * @Desc 检验院级管理员修改项目是否合法切面类
 * @date 2021/11/01 0:06
 */
@Aspect
@Component
@RequiredArgsConstructor
public class AcademyAdminModifyProjectAspect {
    @Lazy
    @Autowired
    private ProjectService projectService;
    @Lazy
    @Autowired
    private AcademyAdminService academyAdminService;
    final CompetitionStageStatusService competitionStageStatusService;

    @Pointcut("@annotation(checkAcademyAdminModifyProject)")
    public void pointCut(CheckAcademyAdminModifyProject checkAcademyAdminModifyProject) throws AppException {
    }

    @Before(value = "pointCut(checkAcademyAdminModifyProject)", argNames = "joinpoint,checkAcademyAdminModifyProject")
    public void before(JoinPoint joinpoint, CheckAcademyAdminModifyProject checkAcademyAdminModifyProject) {
        int userId = StpUtil.getLoginIdAsInt();
        MethodSignature methodSignature = (MethodSignature) joinpoint.getSignature();
        String methodName = methodSignature.getMethod().getName();
        int userAcademyId = academyAdminService.getAcademyIdByUserId(userId);
        String[] parameterNames = methodSignature.getParameterNames();
        switch (methodName){
            //批量提交是多个项目
            case "batchSubmit":
                int compIdx = ArrayUtils.indexOf(parameterNames, "compId");
                int compId = (int) joinpoint.getArgs()[compIdx];
                List<Project> alreadyRecProjects = projectService.getAlreadyRecProjects(userAcademyId, compId);
                for (Project project : alreadyRecProjects) {
                    checkSingleProjectValid(project.getProjectId(), userAcademyId);
                }
                break;

            default:
                int projectIdIdx = ArrayUtils.indexOf(parameterNames, "projectId");
                int projectId = (int) joinpoint.getArgs()[projectIdIdx];
                checkSingleProjectValid(projectId, userAcademyId);
        }

    }

    //检测每单个项目的合法性
    private void checkSingleProjectValid(int projectId, int userAcademyId){
        //确认用户拥有项目修改权限  (同一个学院 + 学校状态为 1：不可见 + 提交和审核阶段)
        ProjectDetailVo projectDetailVo = projectService.getProjectDetailById(projectId);

        //根据项目获得竞赛的提交、审核时间
        int projectAcademyId = projectService.getProjectDetailById(projectId).getAcademyId();
        int projectSchoolStatus = projectDetailVo.getProjectSchoolStatus();
        CompetitionStageStatus SubmitCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectDetailVo.getStageId(),  StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CompetitionStageStatus JudgeCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectDetailVo.getStageId(),  StatusConstant.COMPETITION_STAGE_STATUS_ORDER_JUDGE.getCode());

        //确认用户拥有项目修改权限(同一个学院)
        if(userAcademyId != projectAcademyId){
            throw new AppException(ErrorCode.NOT_PERMITTED);
        }

        //学院信息提交状态为未提交 即学校状态为 1：不可见
        if(projectSchoolStatus != StatusConstant.PROJECT_STATUS_SCHOOL_VERSION_INVISIBLE.getCode()){
            throw new AppException(ErrorCode.PROJECT_HAS_BEEN_SUBMIT);
        }

        //提交和审核阶段
        Date date = new Date();
        if(date.before(SubmitCompStageStatus.getStatusStartTime()) || date.after(JudgeCompStageStatus.getStatusEndTime())){
            throw new AppException(ErrorCode.TIME_CHECK_FAILED);
        }
    }
}
